from hocabul.models import School, Course, Department, UserProfile, EventType, Location, Lesson, LessonsContributors
import fileinput
import re
from collections import defaultdict
from django.db import connection
from django.db import settings
from datetime import datetime
import datetime



#Create school
school = School(name="koc university")
school.save()

# Create event types
et_lesson = EventType.objects.create(abbr='lesson', label='Class lesson')
et2 = EventType.objects.create(abbr='busy', label='Busy Time')

# Create 
loc1 = Location.objects.create(name='kantin', school=school)

# Create courses
departments = {}
courses = {}
department_courses = defaultdict(set)

for line in fileinput.input('../assets/koc_university_courses'):
  matches = re.search(r'(.*?)\t(.*?)\t(.*)', line)
  if matches:
    matches_arr = map(lambda x: x.strip(), matches.groups())
    department = matches_arr[2].lower()
    course_id = matches_arr[0].lower()
    course_name = matches_arr[1].lower()
    if not department in departments:
      departments[department] = Department(school=school, name=department, department_id=department)
    course = Course(name=course_name, course_id=course_id) if course_name not in courses else courses[course_name]
    #Extra-write to courses. Not efficient, but it's fine
    courses[course_name] = course
    department_courses[department].add(course)

for key, department in departments.items():
  department.save()
for key, course in courses.items():
    course.save()
for department, courses in department_courses.items():
  for course in courses: 
    print "adding course %s to department %s" % (course, department)
    try:
      departments[department].courses.add(course)
    except:
      print "fail"
    
# Add instructors
ECON = 'economics'
MATH = 'mathemat.'

instructors = [
('jack', 'ojalvo', [ECON,MATH]),
('eytan', 'daniyalzade', [ECON,MATH]),
('ahmet', 'kazanc',[ECON]),
('naz', 'kaplan', [MATH]),
('berk', 'atikoglu',[MATH])              
]

for instructor in instructors:
  up = UserProfile(first_name=instructor[0], last_name=instructor[1], 
                   username=instructor[0], email=("%s.gmail.com" % instructor[0]),
                   password=instructor[0], school=school)
  up.save()
  for course in instructor[2]:
    department = Department.objects.filter(school=school, name=course)[0]
    all_courses = department.courses.all()
    for course_obj in all_courses: 
      up.courses.add(course_obj)
  
  
# Add lesson
econ_course = Course.objects.filter(course_id='econ. 340')[0]
begin = datetime.datetime(2010, 8, 4, 12, 30, 00)
end = datetime.datetime(2010, 8, 4, 14, 30, 00)
lesson = Lesson.objects.create(name="econ lesson 1", course=econ_course,capacity=5,occupancy=0, location=loc1, 
                               event_type = et_lesson, title='exam preperation', description='exam_prep', 
                               start_time=begin, end_time=end)


user = UserProfile.objects.filter(first_name='jack')[0]
LessonsContributors.objects.create(user=user,lesson=lesson, is_teacher=False)
                                
                               
                               

  
